/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						script_material_quantity.cpp

	$Header: /heroes4/script_material_quantity.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "script_material_quantity.h"

#include "adventure_object.h"
#include "materials.h"
#include "player.h"
#include "script_targeting.h"
#include "sized_int_types.h"
#include "streambuf_operators.h"
#include "town.h"

// --------------------------------------------------------------------------
// t_script_material_quantity members
// --------------------------------------------------------------------------

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
bool t_script_material_quantity::read_from_map(
	std::streambuf &	stream,
	int					format_version )
{
	if ( !t_script_targeted_numeric_expression< t_target >::read_from_map( stream, format_version ) )
		return false;

	m_material = t_material( get< t_uint8 >( stream ) );
	return m_material >= 0 && m_material < k_material_count;
}

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
bool t_script_material_quantity::read(
	std::streambuf &	stream,
	int					format_version )
{
	if ( !t_script_targeted_numeric_expression< t_target >::read( stream, format_version ) )
		return false;

	if (format_version < 3)
		m_material = k_gold;
	else
		m_material = get< t_material >( stream );

	return m_material >= 0 && m_material < k_material_count;
}

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
bool t_script_material_quantity::write( std::streambuf& stream ) const
{
	if ( !t_script_targeted_numeric_expression< t_target >::write( stream ) )
		return false;

	put< t_material >( stream, m_material );
	
	return true;
}

t_script_material_quantity::t_result 
t_script_material_quantity::evaluate(t_expression_context_hero const& context) const
{
	t_player const* player = select_player_by_target(get_target(), context.map, NULL, context.army_owner, context.opposing_army_owner);
	return player ? (player->get_funds())[get_material()] : 0;
}

t_script_material_quantity::t_result 
t_script_material_quantity::evaluate(t_expression_context_town const& context) const
{
	t_player const* player = select_player_by_target(get_target(), context.map, context.object, context.garrison ? context.garrison->get_owner() : NULL, context.opposing_army_owner);
	return player ? (player->get_funds())[get_material()] : 0;
}

t_script_material_quantity::t_result 
t_script_material_quantity::evaluate(t_expression_context_object const& context) const
{
	t_player const* player = select_player_by_target(get_target(), context.map, NULL, NULL, NULL);
	return player ? (player->get_funds())[get_material()] : 0;
}

t_script_material_quantity::t_result 
t_script_material_quantity::evaluate(t_expression_context_army const& context) const
{
	t_player const* player = select_player_by_target(get_target(), context.map, NULL, context.army_owner, context.opposing_army_owner);
	return player ? (player->get_funds())[get_material()] : 0;
}

t_script_material_quantity::t_result 
t_script_material_quantity::evaluate(t_expression_context_global const& context) const
{
	t_player const* player = select_player_by_target(get_target(), context.map, NULL, NULL, NULL);
	return player ? (player->get_funds())[get_material()] : 0;
}

